<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2_react生命周期(旧)</title>
</head>

<body>
    <div id="test"></div>
    <script type="text/javascript"
        src="https://lf26-cdn-tos.bytecdntp.com/cdn/expire-1-M/react/18.2.0/umd/react.production.min.js"></script>
    <script type="text/javascript"
        src="https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/react-dom/18.2.0/umd/react-dom.production.min.js"></script>
    <script type="text/javascript"
        src="https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/babel-standalone/6.26.0/babel.min.js"></script>

    <script type="text/babel">
           /**
         * 1.初始化由ReactDOM.render()触发--初次渲染
         *  - constructor()
         *  - componentWillMount()
         *  - render()
         *  - componentDidMount()
         * 
         * 2.更新阶段: 由组件内部this.setState()或父组件重新render触发
         *  - shouldComponentUpdate()
         *  - componentWillUpdate()
         *  - render()
         *  - componentDidUpdate()
         * 
         * 3.卸载组件: 由ReactDOM.unmountComponentAtNode()触发
         *  - componentWillUnmount()
         *  
         */
        // 创建组件
        class Life extends React.Component {

            constructor(props) {
                console.log("构造器Count-constructor...")
                super(props)
                // 初始化状态
                this.state = { count: 0 }
            }

            //组件将要挂载
            componentWillMount() {
                console.log("Count-componentWillMount...")
            }

            // 组件挂载完毕
            componentDidMount() {
                console.log("Count-componentDidMount...")
            }

            // 组件将要卸载
            componentWillUnmount() {
                console.log("Count-componentWillUnmount...")
            }

            //开关,返回true or false  如果返回false则render方法不起作用
            shouldComponentUpdate(){
                console.info("Count-shouldComponentUpdate...")
                return true
            }

            componentWillUpdate(){
                console.info("Count-componentWillUpdate...")
            }

            componentDidUpdate(){
                console.info("Count-componentDidUpdate...")
            }
            // setState()更新流程:  shouldComponentUpdate -> componentWillUpdate -> render -> componentDidUpdate -> componentWillUnmount

            // render挂载
            render() {
                const { count } = this.state
                console.log("Count-render...")
                return (
                    <div>
                        <h2>当前求和为:{count}</h2>
                        <button onClick={this.add}>点击+1</button>&nbsp;&nbsp;
                        <button onClick={this.death}>卸载组件</button>
                    </div>
                )
            }

            // 加1按钮的回调
            add = () => {
                const { count } = this.state
                this.setState({ count: count + 1 })
            }

            death = ()=>{
                ReactDOM.unmountComponentAtNode(document.getElementById("test"))
            }
        }
        ReactDOM.render(<Life />, document.getElementById("test"))
    </script>
</body>

</html>